# -*- coding: utf-8 -*-

from django.template.response import TemplateResponse
from django.contrib.auth.decorators import login_required
from django.forms.models import modelformset_factory

from django.shortcuts import render_to_response,render
from django.template import RequestContext
from django.http import HttpResponseRedirect, Http404
import math,logging
from django.views.decorators.cache import cache_page

from forms import PaymentForm as CurrentMainForm
from models import  Payment as CurentDataModel 
from models import Employee  
import func

current_app = 'warehouse_pa'
current_url = '/erp/pa/'
title = '支付申请'

@login_required()
def list(request,page=1):   
    #pr_choice = list(PR_STATUS_CHOICES)  
    #logging.error(pr_choice)
    return render_to_response("warehouse/common/list.html",
                              {'app': current_app,'current_url':current_url,
                               'title':title,                           
                               }
                              ,context_instance=RequestContext(request))
@login_required()
def log(request,id):
    po = CurentDataModel.objects.get(pk=id)
    return render_to_response("warehouse/log.html",
                              {'app': current_app,'current_url':current_url,
                               'back_url':current_url+str(id),
                               'title':title,
                               'op_items':po.GetOperatorLog()
                               }
                              ,context_instance=RequestContext(request))
@login_required()
def detail(request,id):
    obj = CurentDataModel.objects.get(pk=id)
    obj.user = request.user
    form = None
    if request.method == 'POST':
        form = CurrentMainForm(request.POST,instance=obj,) 
        logging.error(obj)
        if form.is_valid(): 
            form.save(True)
        else:
            logging.error('form error')
    else:        
        form = CurrentMainForm(instance=obj) 
        pass
    
    if obj.Can_Edit==False:
        for f in ['Title','Customer', 'PurchaseOrder','AuditMan','Amount','PaymentSN','Description']:
            form.fields[f].widget.attrs['readonly'] = True        
    
    return render_to_response("warehouse/common/detail.html",
                              {'title':title,'back_url':current_url,
                               'app': current_app,'current_url':current_url,
                               'form':form, 
                               'id':id,                                
                               'obj':obj,
                               }
                              ,context_instance=RequestContext(request))

@login_required()
def do(request,id,do):     
    o = CurentDataModel.objects.get(pk=id)
    o.user = request.user
    o.Do(request.user,do)
    return HttpResponseRedirect(current_url+id)

from django.contrib.auth.decorators import user_passes_test

@user_passes_test(lambda u: u.id and func.in_group(u,u'财务'))
@login_required()
def add(request):
    if request.method == 'POST': # If the form has been submitted...
        form = CurrentMainForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            o_new = form.save(commit=False)             
            o_new.ApplyMan =  Employee.GetEmployeeFromUser(request.user) 
             
            o_new.save()
            return HttpResponseRedirect(current_url) # Redirect after POST
    else:
        form = CurrentMainForm() # An unbound form
        
    return render_to_response("warehouse/common/dialog.html",
                              {'app': current_app,'current_url':current_url,
                               'title':title,'back_url':current_url,
                               'form':form,
                               'post_url':current_url+'add',
                               'form_title':'新增支付',
                               'form_help':''
                               }
                              ,context_instance=RequestContext(request))
    
    